@page "/admin/login"
@rendermode InteractiveServer
@using DaoHang.Services
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.WebUtilities
@inject IUserService UserService
@inject NavigationManager Navigation
@inject IJSRuntime JSRuntime

<PageTitle>管理员登录 - 导航网站</PageTitle>

<div class="login-container">
    <div class="login-card">
        <div class="login-header">
            <div class="login-icon">
                <i class="bi bi-shield-lock-fill"></i>
            </div>
            <h2>管理员登录</h2>
            <p class="text-muted">请输入您的管理员账号和密码</p>
        </div>

        <EditForm Model="@loginModel" OnValidSubmit="@HandleLogin" class="login-form">
            <DataAnnotationsValidator />
            
            @if (!string.IsNullOrEmpty(errorMessage))
            {
                <div class="alert alert-danger d-flex align-items-center" role="alert">
                    <i class="bi bi-exclamation-triangle-fill me-2"></i>
                    <div>@errorMessage</div>
                </div>
            }

            <div class="form-floating mb-3">
                <InputText @bind-Value="loginModel.Username" 
                          class="form-control"
                          id="username" 
                          placeholder="用户名" 
                          disabled="@isLoading" />
                <label for="username">用户名</label>
                <ValidationMessage For="@(() => loginModel.Username)" class="invalid-feedback" />
            </div>

            <div class="form-floating mb-4">
                <InputText @bind-Value="loginModel.Password" 
                          type="password"
                          class="form-control"
                          id="password" 
                          placeholder="密码" 
                          disabled="@isLoading" />
                <label for="password">密码</label>
                <ValidationMessage For="@(() => loginModel.Password)" class="invalid-feedback" />
            </div>

            <button type="submit" 
                    class="btn btn-primary btn-lg w-100 login-btn" 
                    disabled="@isLoading">
                @if (isLoading)
                {
                    <span class="spinner-border spinner-border-sm me-2" role="status"></span>
                    <span>登录中...</span>
                }
                else
                {
                    <i class="bi bi-box-arrow-in-right me-2"></i>
                    <span>登录</span>
                }
            </button>
        </EditForm>

        <div class="login-footer">
            <a href="/" class="text-decoration-none">
                <i class="bi bi-arrow-left me-1"></i>返回首页
            </a>
        </div>
    </div>
</div>

@code {
    private LoginModel loginModel = new();
    private string errorMessage = string.Empty;
    private bool isLoading = false;

    protected override void OnInitialized()
    {
        // 检查URL中的错误参数
        var uri = Navigation.ToAbsoluteUri(Navigation.Uri);
        var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
        
        if (query.TryGetValue("error", out var error))
        {
            errorMessage = error.ToString() switch
            {
                "invalid" => "用户名或密码错误",
                "server" => "服务器错误，请稍后重试",
                _ => "登录失败，请重试"
            };
        }
    }

    /// <summary>
    /// 登录模型类
    /// </summary>
    public class LoginModel
    {
        [Required(ErrorMessage = "请输入用户名")]
        public string Username { get; set; } = string.Empty;

        [Required(ErrorMessage = "请输入密码")]
        public string Password { get; set; } = string.Empty;
    }

    /// <summary>
    /// 处理登录提交
    /// </summary>
    private void HandleLogin()
    {
        errorMessage = string.Empty;
        isLoading = true;

        try
        {
            // 验证输入
            if (string.IsNullOrEmpty(loginModel.Username) || string.IsNullOrEmpty(loginModel.Password))
            {
                errorMessage = "用户名和密码不能为空";
                isLoading = false;
                return;
            }

            // 直接跳转到登录端点，由API端点处理验证
            var credentials = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{loginModel.Username}:{loginModel.Password}"));
            Navigation.NavigateTo($"/api/auth/login?credentials={credentials}", true);
        }
        catch (Exception)
        {
            errorMessage = "登录过程中发生错误，请稍后重试";
            isLoading = false;
        }
    }
} 